package joke.content;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.database.sqlite.SQLiteDatabase.CursorFactory;

public class JokeDbAdapter {
	public static final String KEY_ROWID = "_id";
	public static final String JOKEID = "jokeid";
	public static final String URL = "url";
	public static final String TITLE = "title";
	public static final String CONTENT = "content";
	public static final String GETTIME = "gettime";
	public static final String STATE = "state";
	public static final String RANK = "rank";
	public static final String SENDTIMES = "sendtimes";
	private DatabaseHelper mDbHelper;
	private SQLiteDatabase mDb;
	private static final String DATABASE_NAME = "happydatabasejoke";
	private static final String DATABASE_TABLE = "joke_content";
	private static final String DATABASE_CREATE = "create table "
			+ DATABASE_TABLE + " (_id integer primary key autoincrement, "
			+ URL + " text, " + JOKEID + " text, " + TITLE + " text , "
			+ CONTENT + " text , " + GETTIME + " text , " + STATE + " text ,"
			+ RANK + " text ," + SENDTIMES + " text );";

	private static final int DATABASE_VERSION = 1;
	private final Context mCtx;

	public JokeDbAdapter(Context ctx) {
		this.mCtx = ctx;
	}

	public JokeDbAdapter open() throws SQLException {
		mDbHelper = new DatabaseHelper(mCtx);
		mDb = mDbHelper.getWritableDatabase();
		return this;
	}

	public void closeclose() {
		mDbHelper.close();
	}

	public boolean deleteDiary(String jokeId) {

		return mDb.delete(DATABASE_TABLE, JOKEID + "='" + jokeId + "'", null) > 0;
	}

	public long insertContent(String jokeid, String title, String content) {
		ContentValues initialValues = new ContentValues();
		initialValues.put(JOKEID, jokeid);
		initialValues.put(TITLE, title);
		initialValues.put(CONTENT, content);
		deleteDiary(jokeid);// 删除重复数据
		return mDb.insert(DATABASE_TABLE, null, initialValues);
	}

	public Cursor getAllNotes() {

		return mDb.query(DATABASE_TABLE, new String[] { KEY_ROWID, JOKEID,
				TITLE, CONTENT }, null, null, null, null, KEY_ROWID + " desc");
	}

	public Cursor getContent(long rowId) throws SQLException {

		Cursor mCursor = mDb.query(true, DATABASE_TABLE, new String[] {
				KEY_ROWID, JOKEID, TITLE, CONTENT }, KEY_ROWID + "=" + rowId,
				null, null, null, null, null);
		if (mCursor != null) {
			mCursor.moveToFirst();
		}
		return mCursor;

	}

	public static class DatabaseHelper extends SQLiteOpenHelper {
		DatabaseHelper(Context context) {
			super(context, DATABASE_NAME, null, DATABASE_VERSION);
		}

		public DatabaseHelper(Context context, String name,
				CursorFactory factory, int version) {
			super(context, name, factory, version);
		}

		@Override
		public void onCreate(SQLiteDatabase db) {
			db.execSQL(DATABASE_CREATE);

		}

		@Override
		public void onUpgrade(SQLiteDatabase db, int arg1, int arg2) {
			db.execSQL("DROP TABLE IF EXISTS " + DATABASE_NAME);
			onCreate(db);
		}
	}

	public Cursor getNextContent(long rowId) throws SQLException {

		Cursor mCursor = mDb.query(DATABASE_TABLE, new String[] { KEY_ROWID,
				JOKEID, TITLE, CONTENT }, KEY_ROWID + "<" + rowId, null, null,
				null, KEY_ROWID + " desc ");
		return mCursor;

	}

	public Cursor getPreContent(long rowId) throws SQLException {
		Cursor mCursor = mDb.query(DATABASE_TABLE, new String[] { KEY_ROWID,
				JOKEID, TITLE, CONTENT }, KEY_ROWID + ">" + rowId, null, null,
				null, KEY_ROWID + " asc");
		return mCursor;

	}
}
